package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.*;
import com.easyui.common.*;

public class GetTakerServlet extends HttpServlet {

	public GetTakerServlet() {}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		List<Student> slist = null;
		JSONArray jsonStudent = null;
		
		//int page = Integer.parseInt(request.getParameter("page"));
		//int rows = Integer.parseInt(request.getParameter("rows"));//page和rows需要返回
		String orderBy = request.getParameter("order");//排序方式,不用返回
		String sortName = request.getParameter("sort");//排序字段,不用返回
		
		
		HttpSession session = request.getSession(); //获的session
		String role = (String)session.getAttribute("role");
		String tno = (String)session.getAttribute("userID");
		
		SelStudent ss = new SelStudent();
		if(role != null && role.trim().equals("teacher")) {//登录身份为老师
			if(tno != null && !tno.trim().equals("")) {//获取到该老师工号
				slist = ss.getMStudents(tno, orderBy, sortName);//返回所有学生信息
			}
		}
		
		if (slist != null && slist.size() > 0) {
			jsonStudent = new JSONArray();//装学生信息
			for (int i = 0; i < slist.size(); i++) {
				Student s = (Student)slist.get(i);
				int proNum = 0;
				int speProNum = 0;
				JSONObject j = new JSONObject();
				j.put("sno", s.getSno());
				j.put("sname", s.getSname());
				j.put("sex", s.getSex());
				j.put("major", s.getMajor());
				j.put("grade", s.getGrade());
				j.put("stype", s.getStype());
				
				if(s.getProNum() == null) { 
					proNum = 0;
				} else {
					proNum = s.getProNum().size();
				}
				j.put("proNum", proNum);
				
				List<Integer> speProList = null;
				speProList = new SelProject().getSpeProList(s.getSno(), tno);
				if(speProList != null && speProList.size() > 0) {
					speProNum = speProList.size();
				} else {
					speProNum = 0;
				}
				
				//System.out.println("sno:" + s.getSno());
				//System.out.println("speProNum:" + speProNum);
				j.put("speProNum", speProNum);
				
				jsonStudent.add(j);
			}
			
		} else {
		}
		
		out.print(jsonStudent);
		out.flush();
		out.close();
	}


}
